package com.fjec.common.aliyun.api.view;

import com.google.common.base.Strings;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 阿里云身份证识别返回数据
 * 正面返回结果：
 * {
 * "address"    : "浙江省杭州市余杭区文一西路969号",   #地址信息
 * "config_str" : "{\\\"side\\\":\\\"face\\\"}",    #配置信息，同输入configure
 * "face_rect":{       #人脸位置
 * "angle": -90,   #angle表示矩形顺时针旋转的度数
 * "center":{      #center表示人脸矩形中心坐标
 * "x" : 952,
 * "y" : 325.5
 * },
 * "size":{        #size表示人脸矩形长宽
 * "height":181.99,
 * "width":164.99
 * }
 * },
 * "card_region":[  #身份证区域位置，四个顶点表示，顺序是逆时针(左上、左下、右下、右上)
 * {"x":165,"y":657},
 * {"x":534,"y":658},
 * {"x":535,"y":31},
 * {"x":165,"y":30}
 * ],
 * "face_rect_vertices":[  #人脸位置，四个顶点表示
 * {
 * "x":1024.6600341796875,
 * "y":336.629638671875
 * },
 * {
 * "x":906.66107177734375,
 * "y":336.14801025390625
 * },
 * {
 * "x":907.1590576171875,
 * "y":214.1490478515625
 * },
 * {
 * "x":1025.157958984375,
 * "y":214.63067626953125
 * }
 * ],
 * "name" : "张三",                 #姓名
 * "nationality": "汉"，            #民族
 * "num" : "1234567890",            #身份证号
 * "sex" : "男",                    #性别
 * "birth" : "20000101",            #出生日期
 * "is_fake": false,                   #是否是复印件
 * "success" : true                 #识别结果，true表示成功，false表示失败
 * }
 * <p>
 * <p>
 * 反面返回结果:
 * {
 * "config_str" : "{\\\"side\\\":\\\"back\\\"}",  #配置信息，同输入configure
 * "start_date" : "19700101",       #有效期起始时间
 * "end_date" : "19800101",         #有效期结束时间
 * "issue" : "杭州市公安局",         #签发机关
 * "is_fake": false,                   #是否是复印件
 * "success" : true                 #识别结果，true表示成功，false表示失败
 * }
 */
public class IdCardVO extends HashMap<String, Object> {

    private final static String SUCCESS_KEY = "success";
    private final static String CONFIG_STR_KEY = "config_str";
    private final static String NAME_KEY = "name";
    private final static String BIRTH_KEY = "birth";
    private final static String SEX_KEY = "sex";
    private final static String NUM_KEY = "num";
    private final static String ADDRESS_KEY = "address";
    private final static String NATIONALITY_KEY = "nationality";
    private final static String START_DATE_KEY = "start_date";
    private final static String END_DATE_KEY = "end_date";
    private final static String ISSUE_KEY = "issue";
    private final static String IS_FAKE_KEY = "is_fake";

    public static IdCardVO build(Map<String, Object> map) {
        IdCardVO vo = new IdCardVO();
        vo.putAll(map);
        return vo;
    }

    private IdCardVO() {
    }

    /**
     * 是否调用成功
     *
     * @return
     */
    public boolean isSuccess() {
        return MapUtils.getBooleanValue(this, SUCCESS_KEY);
    }

    /**
     * 正面还是反面 0=正面， 1=反面, -1=错误
     *
     * @return
     */
    public Integer faceOrBack() {
        String configStr = MapUtils.getString(this, CONFIG_STR_KEY);
        if (configStr.contains("face")) {
            return 0;
        } else if (configStr.contains("back")) {
            return 1;
        } else {
            return -1;
        }
    }

    /**
     * 姓名
     *
     * @return
     */
    public String getName() {
        if (faceOrBack() == 0) {
            return MapUtils.getString(this, NAME_KEY);
        } else {
            return "";
        }
    }

    /**
     * 出生日期
     *
     * @return
     */
    public String getBirth() {
        if (faceOrBack() == 0) {
            return MapUtils.getString(this, BIRTH_KEY);
        } else {
            return "";
        }
    }

    /**
     * 性别 0=女 1=男 -1=错误
     *
     * @return
     */
    public int getSex() {
        if (faceOrBack() == 0) {
            String strSex = MapUtils.getString(this, SEX_KEY);
            if (Objects.equals(strSex, "女")) {
                return 0;
            } else if (Objects.equals(strSex, "男")) {
                return 1;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

    /**
     * 身份证号
     *
     * @return
     */
    public String getNum() {
        if (faceOrBack() == 0) {
            return MapUtils.getString(this, NUM_KEY);
        } else {
            return "";
        }
    }

    /**
     * 住址
     *
     * @return
     */
    public String getAddress() {
        if (faceOrBack() == 0) {
            return MapUtils.getString(this, ADDRESS_KEY);
        } else {
            return "";
        }
    }

    /**
     * 民族
     *
     * @return
     */
    public String getNationality() {
        if (faceOrBack() == 0) {
            return MapUtils.getString(this, NATIONALITY_KEY);
        } else {
            return "";
        }
    }

    /**
     * 有效期起始时间
     *
     * @return
     */
    public String getStartDate() {
        if (faceOrBack() == 1) {
            return MapUtils.getString(this, START_DATE_KEY);
        } else {
            return "";
        }
    }

    /**
     * 有效期结束时间
     *
     * @return
     */
    public String getEndDate() {
        if (faceOrBack() == 1) {
            return MapUtils.getString(this, END_DATE_KEY);
        } else {
            return "";
        }
    }

    /**
     * 签发机关
     *
     * @return
     */
    public String getIssue() {
        if (faceOrBack() == 1) {
            return MapUtils.getString(this, ISSUE_KEY);
        } else {
            return "";
        }
    }

    /**
     * 是否是复印件
     *
     * @return
     */
    public boolean isFake() {
        return MapUtils.getBooleanValue(this, IS_FAKE_KEY);
    }

    /**
     * 校验证件有效性
     *
     * @return
     */
    public boolean isValid() {
        if (Strings.isNullOrEmpty(getName()) || Strings.isNullOrEmpty(getNationality())
                || Strings.isNullOrEmpty(getNum()) || Strings.isNullOrEmpty(getAddress())
                || getSex() == -1 || isFake()) {
            return false;
        } else {
            return true;
        }
    }
}
